package com.eboss2.sbs.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.API;
import com.eboss2.sbs.http.HttpCallback;
import com.eboss2.sbs.model.GgList;
import com.eboss2.sbs.model.TempGgInfo;
import com.eboss2.sbs.model.WalletAccountInfo;
import com.google.gson.reflect.TypeToken;
import com.lzy.imagepicker.ImagePicker;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyListView;
import com.tool.utils.utils.GsonUtil;
import com.tool.utils.utils.GsonUtils;
import com.tool.utils.utils.SPUtils;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;
import com.tool.utils.view.MyGridView;
import com.tool.utils.view.RTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MySpggActivity extends BaseActivity implements View.OnClickListener{


	private List<String> ggName1 = new ArrayList<String>();
	private List<String> ggName2 = new ArrayList<String>();
	private List<GgList> ggListInfos = new ArrayList<GgList>();

	private TempGgInfo tempGgInfo = null;

	private RTextView add_gg1;
	private RTextView add_gg2;
	private EditText et_gg1;
	private EditText et_gg2;
	private TextView tv_gg1;
	private TextView tv_gg2;
	private MyGridView id_gridview1;
	private MyGridView id_gridview2;
	private MyListView listview;

	private TextView tv_cancel;
	private TextView tv_save;

	private AdapterGg adapterGg1;
	private AdapterGg adapterGg2;
	private AdapterGgList adapterGgList;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_spgg);
		MyActivityManager.getInstance().addActivity(this);
		initView();
		initData();
	}

	private void initData() {

		tempGgInfo = (TempGgInfo) getIntent().getSerializableExtra(Config.GG_FILE_NAME);
		if (tempGgInfo != null){
			if (!StringUtils.isBlank(tempGgInfo.getGg1())) {
				et_gg1.setText(tempGgInfo.getGg1());
			}
			if (!StringUtils.isBlank(tempGgInfo.getGg2())) {
				et_gg2.setText(tempGgInfo.getGg2());
			}
			if (tempGgInfo.getGgName1() != null && tempGgInfo.getGgName1().size() > 0) {
				ggName1.addAll(tempGgInfo.getGgName1());
			}
			if (tempGgInfo.getGgName2() != null && tempGgInfo.getGgName2().size() > 0){
				ggName2.addAll(tempGgInfo.getGgName2());
			}
			if (tempGgInfo.getGgLists() != null && tempGgInfo.getGgLists().size() > 0){
				ggListInfos.addAll(tempGgInfo.getGgLists());
			}

		}


//		String etGg1 = (String) SPUtils.get(mContext, Config.GG_FILE_NAME, "et_gg1", "");
//		String etGg2 = (String) SPUtils.get(mContext, Config.GG_FILE_NAME, "et_gg2", "");
//
//		et_gg1.setText(etGg1);
//		et_gg2.setText(etGg2);
//
//		String str_ggName1 = (String) SPUtils.get(mContext, Config.GG_FILE_NAME, "str_ggName1", "");
//		String str_ggName2 = (String) SPUtils.get(mContext, Config.GG_FILE_NAME, "str_ggName2", "");
//		String str_ggListInfos =(String) SPUtils.get(mContext, Config.GG_FILE_NAME, "str_ggListInfos", "");
//		if (!StringUtils.isBlank(str_ggName1)) {
//			List<String> data1 = (List<String>) GsonUtils.parseJsonToList(str_ggName1, new TypeToken<List<String>>() {
//			}.getType());
//			ggName1.clear();
//			ggName1.addAll(data1);
//
//		}
//		if (!StringUtils.isBlank(str_ggName2)) {
//			List<String> data2 = (List<String>) GsonUtils.parseJsonToList(str_ggName2, new TypeToken<List<String>>() {
//			}.getType());
//			ggName2.clear();
//			ggName2.addAll(data2);
//		}
//		if (!StringUtils.isBlank(str_ggListInfos)) {
//			List<GgList> ggLists = (List<GgList>) GsonUtils.parseJsonToList(str_ggListInfos, new TypeToken<List<GgList>>() {
//			}.getType());
//			ggListInfos.clear();
//			ggListInfos.addAll(ggLists);
//		}
		adapterGg1.notifyDataSetChanged();
		adapterGg2.notifyDataSetChanged();
		adapterGgList.notifyDataSetChanged();
	}

	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "商城规格");
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);

		add_gg1 = findViewById(R.id.add_gg1);
		add_gg2 = findViewById(R.id.add_gg2);
		add_gg1.setOnClickListener(this);
		add_gg2.setOnClickListener(this);

		tv_cancel = textView(R.id.tv_cancel);
		tv_cancel.setOnClickListener(this);
		tv_save = textView(R.id.tv_save);
		tv_save.setOnClickListener(this);

		et_gg1 = editText(R.id.et_gg1);
		et_gg1.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void afterTextChanged(Editable editable) {
				tv_gg1.setText(et_gg1.getText().toString());


			}
		});
		et_gg2 = editText(R.id.et_gg2);
		et_gg2.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

			}

			@Override
			public void afterTextChanged(Editable editable) {
				tv_gg2.setText(et_gg2.getText().toString());
			}
		});

		tv_gg1 = textView(R.id.tv_gg1);
		tv_gg2 = textView(R.id.tv_gg2);

		id_gridview1 = findViewById(R.id.id_gridview1);
		id_gridview2 = findViewById(R.id.id_gridview2);
		listview = findViewById(R.id.listview);

		adapterGg1 = new AdapterGg(mContext, ggName1, new AdapterGg.ListenerGg() {
			@Override
			public void itemListener(int position) {
				ggName1.clear();
				getEditText(ggName1, id_gridview1, position);
				adapterGg1.notifyDataSetChanged();
				setGgListInfo();
			}

			@Override
			public void updateData() {
//				ggName1.clear();
				if(delayRun1!=null){
					//每次editText有变化的时候，则移除上次发出的延迟线程
					handler.removeCallbacks(delayRun1);
				}


				//延迟800ms，如果不再输入字符，则执行该线程的run方法
				handler.postDelayed(delayRun1, 1000);
			}
		});
		id_gridview1.setAdapter(adapterGg1);

		adapterGg2 = new AdapterGg(mContext, ggName2, new AdapterGg.ListenerGg() {
			@Override
			public void itemListener(int position) {
				ggName2.clear();
				getEditText(ggName2, id_gridview2, position);
				adapterGg2.notifyDataSetChanged();
				setGgListInfo();
			}

			@Override
			public void updateData() {

				if(delayRun2!=null){
					//每次editText有变化的时候，则移除上次发出的延迟线程
					handler.removeCallbacks(delayRun2);
				}


				//延迟800ms，如果不再输入字符，则执行该线程的run方法
				handler.postDelayed(delayRun2, 1000);
//				ggName2.clear();

			}
		});
		id_gridview2.setAdapter(adapterGg2);

		adapterGgList = new AdapterGgList(mContext, ggListInfos);
		listview.setAdapter(adapterGgList);

	}

	private Handler handler = new Handler();

	/**
	 * 延迟线程，看是否还有下一个字符输入
	 */
	private Runnable delayRun1 = new Runnable() {

		@Override
		public void run() {
			ggName1.clear();
			getEditText(ggName1, id_gridview1, -1);
			setGgListInfo();
		}
	};
	private Runnable delayRun2 = new Runnable() {

		@Override
		public void run() {
			ggName2.clear();
			getEditText(ggName2, id_gridview2, -1);
			setGgListInfo();
		}
	};

	private void getEditText(List<String> ggName, MyGridView gridview, int position){

		for (int i = 0; i < gridview.getChildCount(); i++){
			if (i != position) {
				RelativeLayout layout = (RelativeLayout) gridview.getChildAt(i);
				EditText et = (EditText) layout.findViewById(R.id.et_gg_name);
				ggName.add(et.getText().toString().trim());
			}
		}
	}

	private void setGgListInfo(){
		ggListInfos.clear();
		if (ggName1.size() > 0) {
			for (int i = 0; i < ggName1.size(); i++) {
				if (ggName2.size() > 0 ) {
					for (int j = 0; j < ggName2.size(); j++) {
						if (!StringUtils.isBlank(ggName2.get(j))) {
							GgList ggList = new GgList();
							ggList.setGgOne(ggName1.get(i));
							ggList.setGgTwo(ggName2.get(j));

							ggListInfos.add(ggList);
						}else{

							GgList ggList = new GgList();
							ggList.setGgOne(ggName1.get(i));
							ggListInfos.add(ggList);
						}
					}
				}else{
					GgList ggList = new GgList();
					ggList.setGgOne(ggName1.get(i));
					ggListInfos.add(ggList);
				}
			}
		}else {
			for (int i = 0; i < ggName2.size(); i++) {
				GgList ggList = new GgList();
				ggList.setGgTwo(ggName2.get(i));
				ggListInfos.add(ggList);
			}
		}
		adapterGgList.notifyDataSetChanged();
	}

	

	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("sid", mApp.getLoginEntity().getSid());
		API.http_post(this, API.WALLET_ACCOUNT, params, new HttpCallback<WalletAccountInfo>(this) {
			@Override
			public void onSuccess(int statusCode, WalletAccountInfo data) {

			}

			@Override
			public void onFailure(int statusCode, String message) {
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<WalletAccountInfo>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
			case R.id.tv_cancel:
				finish();
				break;
			case R.id.tv_save:
//				String str_ggName1 = GsonUtils.parseObjToJson(ggName1);
//				String str_ggName2 = GsonUtils.parseObjToJson(ggName2);
//				String str_ggListInfos = GsonUtils.parseObjToJson(ggListInfos);
//				SPUtils.put(mContext, Config.GG_FILE_NAME, "str_ggName1", str_ggName1);
//				SPUtils.put(mContext, Config.GG_FILE_NAME, "str_ggName2", str_ggName2);
//				SPUtils.put(mContext, Config.GG_FILE_NAME, "str_ggListInfos", str_ggListInfos);
//				SPUtils.put(mContext, Config.GG_FILE_NAME, "et_gg1", et_gg1.getText().toString().trim());
//				SPUtils.put(mContext, Config.GG_FILE_NAME, "et_gg2", et_gg2.getText().toString().trim());
				tempGgInfo = new TempGgInfo();
				tempGgInfo.setGg1(et_gg1.getText().toString().trim());
				tempGgInfo.setGg2(et_gg2.getText().toString().trim());
				tempGgInfo.setGgName1(ggName1);
				tempGgInfo.setGgName2(ggName2);
				tempGgInfo.setGgLists(ggListInfos);
				Intent intent = new Intent();
				intent.putExtra(Config.GG_FILE_NAME, tempGgInfo);
				setResult(4, intent);   //单选不需要裁剪，返回数据
				finish();
				break;
			case R.id.add_gg1:
				if (StringUtils.isBlank(et_gg1.getText().toString().trim())){
					ToastUtils.showShort(mContext, "请输入规格1");
					return;
				}
				ggName1.add("");
				adapterGg1.notifyDataSetChanged();
				break;
			case R.id.add_gg2:
				if (StringUtils.isBlank(et_gg2.getText().toString().trim())){
					ToastUtils.showShort(mContext, "请输入规格2");
					return;
				}
				ggName2.add("");
				adapterGg2.notifyDataSetChanged();
				break;
			default:break;
		}
	}

	public static class AdapterGg extends BaseAdapter {

		private Context context;
		private List<String> ggName = new ArrayList<String>();
		private ListenerGg listenerGg;

		interface ListenerGg{
			void itemListener(int position);
			void updateData();
		}

		public AdapterGg(Context context, List<String> ggName, ListenerGg listenerGg) {
			this.context = context;
			this.ggName = ggName;
			this.listenerGg = listenerGg;
		}

		@Override
		public int getCount() {
			return ggName == null ? 0 : ggName.size();
		}

		@Override
		public Object getItem(int position) {
			return ggName.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.listitem_gg, null);

				holder.imageView = (ImageView) convertView.findViewById(R.id.id_close);
				holder.et_name = (EditText) convertView.findViewById(R.id.et_gg_name);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (ggName.size() >0) {
				if (!StringUtils.isBlank(ggName.get(position))) {
					holder.et_name.setText(ggName.get(position));
				}else{
					holder.et_name.setText("");
				}
			}
			holder.et_name.addTextChangedListener(new TextWatcher() {
				@Override
				public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

				}

				@Override
				public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

				}

				@Override
				public void afterTextChanged(Editable editable) {
					listenerGg.updateData();
				}
			});

			holder.imageView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					listenerGg.itemListener(position);
				}
			});


			return convertView;
		}

		private class ViewHolder {

			ImageView imageView;
			EditText et_name;
		}
	}


	public static class AdapterGgList extends BaseAdapter {

		private Context context;
		private List<GgList> ggLists = new ArrayList<GgList>();



		public AdapterGgList(Context context, List<GgList> ggLists) {
			this.context = context;
			this.ggLists = ggLists;
		}

		@Override
		public int getCount() {
			return ggLists == null ? 0 : ggLists.size();
		}

		@Override
		public Object getItem(int position) {
			return ggLists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(R.layout.listitem_gg_list, null);

				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.tv_name2 = (TextView) convertView.findViewById(R.id.tv_name2);
				holder.et_gg_1 = (EditText) convertView.findViewById(R.id.et_gg_1);
				holder.et_gg_2 = (EditText) convertView.findViewById(R.id.et_gg_2);
				holder.et_gg_3 = (EditText) convertView.findViewById(R.id.et_gg_3);
				holder.et_gg_4 = (EditText) convertView.findViewById(R.id.et_gg_4);
				holder.et_gg_5 = (EditText) convertView.findViewById(R.id.et_gg_5);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			GgList ggList = ggLists.get(position);
			holder.tv_name.setText(ggList.getGgOne());
			holder.tv_name2.setText(ggList.getGgTwo());


			return convertView;
		}

		private class ViewHolder {

			TextView tv_name;
			TextView tv_name2;
			EditText et_gg_1;
			EditText et_gg_2;
			EditText et_gg_3;
			EditText et_gg_4;
			EditText et_gg_5;
		}
	}
}
